<?php

namespace app\index\model;

use think\Db;
use think\Model;

/**
 * 游戏包-模型
 * @author ZhangXuanNeng
 * @since 2024/08/24
 * Class Package
 * @package app\api\model
 */
class Package extends Model
{
    /**
     * 获取游戏分端下载信息
     * @param $package_id
     * @return Package
     */
    public function getPackageDownInfo($package_id,$promote_id){
        $relation_package_id = $this->where('id',$package_id)->value('relation_package_id');
        $rows = $this->where('relation_package_id',$relation_package_id)
            ->field('id,sdk_version,relation_package_name,icon')
            ->select();
        $data = [
            'anDownUrl' => '',
            'pcDownUrl' => '',
            'iosDownUrl'=> '',
        ];
        $ids = [];
        foreach ($rows as $k=>$v){
            if(!isset($data['relation_package_name'])){
                $data['relation_package_name'] = $v['relation_package_name'];
            }
            if(!isset($data['icon'])){
                $data['icon'] = $v['icon'];
            }
            $ids[] = $v['id'];
        }
        $apply = Db::name('apply')
            ->where('package_id','in',$ids)
            ->where('promote_id',$promote_id)
            ->field('id,status,enable_status,pack_url,plist_url,sdk_version,package_landing_template_id')
            ->select();
        $package_landing_template_id = Db::name('package_config')
            ->where('package_id',$package_id)->value('package_landing_template_id');
        foreach ($apply as $k=>$v){
            if($v['enable_status'] == 1){
                if($v['sdk_version'] == 1){
                    $data['anDownUrl'] = $v['pack_url'];
                }
                if($v['sdk_version'] == 2){
                    $data['iosDownUrl'] = "itms-services://?action=download-manifest&url={$v['plist_url']}";
                }
                if($v['sdk_version'] == 4){
                    $data['pcDownUrl'] = $v['pack_url'];
                    if($v['package_landing_template_id']){
                        $package_landing_template_id = $v['package_landing_template_id'];
                    }
                }
            }
        }
        if($package_landing_template_id){
            $template = Db::name('package_landing_template')->where('id',$package_landing_template_id)->find();
            $path =  "uploads".$template['path'];
        }else{
            $path = "uploads/template/webdefault";
        }
        $data['path'] = $path;
        return $data;
    }
}
